<?php

namespace App\Repositories;

use Carbon\Carbon;
use App\Enums\CrossRoomPkEnum;
use App\Exceptions\BasicException;

class CrossRoomPkBlackRepository
{
    /**
     * @Desc:跨房PK黑名单列表
     * @param $param
     * @param $page
     * @param $perPage
     * @return mixed
     * @author: shiquan
     * @Time: 2022/9/19 16:33
     */
    public function list($param, $page = 1, $perPage = 10)
    {
        $list = app('model_cross_room_pk_black')->from('ln_cross_room_pk_blacklist as b')->join('ln_user_room as r','b.room_id','=','r.id');

        $list->whereBetween('b.created_at', [$param['start_at'], $param['end_at']]);

        if (isset($param['room_id']) && !empty($param['room_id'])) {
            $list = $list->where('b.room_id', $param['room_id']);
        }

        if (isset($param['room_name']) && !empty($param['room_name'])) {
            $list = $list->where('b.room_name', 'like', '%' . $param['room_name'] . '%');
        }

        $list = $list->select(
            'b.id',
            'r.room_id',
            'b.room_name',
            'b.room_user_id',
            'b.reason',
            'b.admin_id',
            'b.created_at'
        )->orderBy('id', 'desc')->paginate($perPage, ['*'], '', $page);

        return $list;
    }

    /**
     * @Desc:详情
     * @param $roomId
     * @return array
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/19 16:34
     */
    public function info($roomId)
    {
        //根据房间号查询到房间信息
        $room = app('model_user_room')->select(['id', 'user_id', 'room_name', 'room_id', 'holder'])->where('room_id', $roomId)->first();
        if (empty($room)) throw new BasicException(96200);
        //根据用户ID查询公会信息
        $sociatyUser = app('model_sc_sociaty_user')->where('user_id', $room['user_id'])->with('sociaty:id,sociaty_name')->first();
        $data = [
            'id' => $room['id'],    //房间id
            'user_id' => $room['user_id'],
            'room_id' => $room['room_id'],//房间号
            'room_name' => $room['room_name'],
            'sociaty_name' => '',
            'holder' => $room['holder']
        ];
        if (!empty($sociatyUser)){
            $data['sociaty_name'] = $sociatyUser->sociaty->sociaty_name;
        }
        return $data;
    }

    /**
     * @Desc:新增
     * @param $param
     * @return array
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/19 16:34
     */
    public function create($param)
    {
        $data = $this->info($param['room_id']);
        $insertBlackData = [
            'room_id' => $data['id'],
            'room_user_id' => $data['user_id'],
            'room_name' => $data['room_name'],
            'reason' => $param['reason'] ?? '',
            'admin_id' => $param['admin_id'],
        ];
        $insertBlackLogData = [
            'room_id' => $data['room_id'],
            'room_name' => $data['room_name'],
            'reason' => $param['reason'] ?? '',
            'admin_id' => $param['admin_id'],
            'created_at' => Carbon::now()->toDateTimeString()
        ];
        $info = app('model_cross_room_pk_black')->where('room_id', $data['id'])->first();
        if ($info) {
            throw new BasicException(96203);
        }
        //开启事务执行创建的操作
        \DB::beginTransaction();
        try {
            app('model_cross_room_pk_black')->create($insertBlackData);
            app('model_cross_room_pk_black_log')->create($insertBlackLogData);
            //拉入黑名单就相当于封禁房间了
            app('repo_cross_room_pk')->abnormaHandle($data['id'], CrossRoomPkEnum::ABNORMAL_END);

            \DB::commit();
        } catch (\Exception $e) {

            \DB::rollBack();
            throw new BasicException(96201);
        }
        //发系统消息不应该影响主流程

        //1.给房主发送系统消息
        app('repo_user_news')->userNewsAdd([
            'user_id'  => $data['user_id'],
            'content'  => '因'.$data['room_name'].'房间近期在PK中有多次不良流局记录，'.$data['room_name'].'房间已被系统加入PK黑名单，暂不能发起PK和接受邀请。如有疑问请联系平台客服。',
            'add_time' => time(),
            'title'    => '系统消息提示',
        ]);
        //2.给主持发系统消息
        if ($data['holder'] > 0){
            app('repo_user_news')->userNewsAdd([
                'user_id'  => $data['holder'],
                'content'  => '因'.$data['room_name'].'房间近期在PK中有多次不良流局记录，'.$data['room_name'].'房间已被系统加入PK黑名单，暂不能发起PK和接受邀请。如有疑问请联系平台客服。',
                'add_time' => time(),
                'title'    => '系统消息提示',
            ]);
        }
        return $insertBlackData;
    }

    /**
     * @Desc:删除--移除黑名单
     * @param $param
     * @return bool
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/19 16:34
     */
    public function delete($param)
    {
        $info = app('model_cross_room_pk_black')->where('id', $param['id'])->first()->toArray();
        $insertBlackLogData = [
            'room_id' => $info['room_id'],
            'room_name' => $info['room_name'],
            'reason' => $param['reason'] ?? '',
            'admin_id' => $param['admin_id'],
            'created_at' => Carbon::now()->toDateTimeString(),
        ];
        //开启事务执行创建的操作
        \DB::beginTransaction();
        try {
            app('model_cross_room_pk_black')->where('id', $param['id'])->delete();
            app('model_cross_room_pk_black_log')->create($insertBlackLogData);
            \DB::commit();
        } catch (\Exception $e) {
            \DB::rollBack();
            throw new BasicException(96202);
        }
        //发送系统消息不应影响主流程
        $room = app('model_user_room')->select(['id', 'user_id', 'holder', 'room_name'])->where('id', $info['room_id'])->first();
        if (!empty($room)){
            //1.给房主发送系统消息
            app('repo_user_news')->userNewsAdd([
                'user_id'  => $room['user_id'],
                'content'  => '系统已将'.$room['room_name'].'房间移出PK黑名单，您可以在此房间正常使用PK功能。如有疑问请联系平台客服',
                'add_time' => time(),
                'title'    => '系统消息提示',
            ]);
            //2.给主持发系统消息
            if ($room['holder'] > 0){
                app('repo_user_news')->userNewsAdd([
                    'user_id'  => $room['holder'],
                    'content'  => '系统已将'.$room['room_name'].'房间移出PK黑名单，您可以在此房间正常使用PK功能。如有疑问请联系平台客服',
                    'add_time' => time(),
                    'title'    => '系统消息提示',
                ]);
            }
        }
        return true;
    }
}
